package cn.hiapi.shopkeeper.service.order;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.jpa.pageable.QueryWrapper;
import cn.hiapi.basic.service.AbsBasicService;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shopkeeper.entity.activity.ActivityOther;
import cn.hiapi.shopkeeper.entity.order.ActivityInviteRecord;
import cn.hiapi.shopkeeper.enums.ActivityType;
import cn.hiapi.shopkeeper.jpa.order.ActivityInviteRecordJpa;
import cn.hiapi.shopkeeper.service.activity.ActivityFactory;
import cn.hiapi.shopkeeper.service.activity.ActivityOtherService;
import cn.hiapi.utils.DateUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author AdinZ
 * @date 2023/1/10 10:20
 */
@Service
public class ActivityInviteRecordService extends AbsBasicService<ActivityInviteRecordJpa, ActivityInviteRecord, Long> {

    private final String KEY = "CORE:INVITE_EXISTS";
    private final DispatchContext dispatchContext;

    public ActivityInviteRecordService(DispatchContext dispatchContext) {
        this.dispatchContext = dispatchContext;
    }


    /**
     * 我的抽奖次数
     *
     * @param uid 用户id
     * @param aid 活动id
     * @return
     */
    public long lotteryCount(long uid, long aid) {
        long count = super.count(
                new QueryWrapper().eq("inviteUid", uid)
                        .eq("aid", aid)
                        .eq("type", ActivityType.LOTTERY)
                        .eq("status", 0)
        );

        return count;
    }

    public List<ActivityInviteRecord> lotteryList(long uid, long aid) {
        List<ActivityInviteRecord> list = super.findList(
                new QueryWrapper().eq("inviteUid", uid)
                        .eq("aid", aid)
                        .eq("type", ActivityType.LOTTERY)
                        .eq("status", 0)
        );
        return list;
    }


    /**
     * 统计总得到分享金额
     *
     * @param inviteUid
     * @param aid
     * @return
     */
    public BigDecimal totalShareAmount(long inviteUid, long aid) {
        return ResUtils.getDefault(this.repository.totalShareAmount(inviteUid, aid));
    }


    /**
     * 获取今日邀请总人数
     *
     * @param inviteUid
     * @param aid
     * @return
     */
    public long todayInviteCount(long inviteUid, long aid) {
        List list = this.repository.todayInviteCount(inviteUid, aid, DateUtils.todayStart(0).getTime(), DateUtils.todayEnd(0).getTime());
        if (list == null) return 0;
        return list.size();
    }
}
